package com.easyboot.web.monitor.service;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyboot.framework.domain.DataTableDto;
import com.easyboot.framework.exception.EasyException;
import com.easyboot.framework.shiro.UserOwnInfo;
import com.easyboot.web.monitor.domain.MonitorJob;
import com.easyboot.web.monitor.jobutils.ScheduleUtils;
import com.easyboot.web.monitor.mapper.MonitorJobMapper;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public  class MonitorJobService{
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private MonitorJobMapper jobMapper;

    /**
     *
     * @author yushuo
     * @description //任务列表
     * @date 16:38 2021/2/1
     * @param [dto, roleName, roleCode]
     * @return com.easycms.framework.domain.DataTableDto
     **/
    public DataTableDto getPageList(DataTableDto dto){
        LambdaQueryWrapper<MonitorJob> query=new LambdaQueryWrapper<>();

        query.orderByAsc(MonitorJob::getJobId);
        Page<MonitorJob> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        IPage<MonitorJob> userIPage = jobMapper.selectPage(page, query);
        dto.setTotal(userIPage.getTotal());
        dto.setRows(userIPage.getRecords());
        return dto;
    }


    /**
     * 获取monitorJob数据
     * @param id
     * @return
     */
    public MonitorJob getModel(long id){
        //LambdaQueryWrapper<MonitorJob> query= Wrappers.lambdaQuery();
       // query.eq(MonitorJob::getJobId,id);
        MonitorJob job= jobMapper.selectById(id);
        return job;
    }

    /**
     * @author yushuo
     * @description 保存任务
     * @date 14:38 2021/6/22
     * @param [job]
     * @return void
    **/
    public void saveJob(MonitorJob job, UserOwnInfo userOwnInfo)throws SchedulerException, EasyException {


        job.setUpdateTime(DateUtil.date());
        job.setUpdateBy(userOwnInfo.getLoginName());
        if(job.getJobId()==0){
            job.setCreateBy(userOwnInfo.getLoginName());
            job.setCreateTime(DateUtil.date());
            int rows=jobMapper.insert(job);
            if(rows>0){
                ScheduleUtils.createScheduleJob(scheduler,job);
            }
        }else{
            int rows=jobMapper.updateById(job);
            if(rows>0){
                ScheduleUtils.createScheduleJob(scheduler,job);
            }
        }
    }

    /**
     * @author yushuo
     * @description 停止任务
     * @date 14:41 2021/6/23
     * @param [id]
     * @return void
     **/
    public void stopJob(long id) throws SchedulerException{
        MonitorJob job=getModel(id);
        if(job==null){
            return;
        }
         if(job.getJobStatus()==1){
             ScheduleUtils.pauseJob(scheduler,job);
             LambdaUpdateWrapper<MonitorJob> update= Wrappers.lambdaUpdate();
             update.set(MonitorJob::getJobStatus,0)
                     .eq(MonitorJob::getJobId,id);
             jobMapper.update(null,update);
         }

    }

    /**
    *
     * @author yushuo
     * @description 执行一次任务
     * @date 15:24 2021/6/23
     * @param [id]
     * @return void
    **/
    public void runJob(long id) throws SchedulerException{
        MonitorJob job=getModel(id);
        if(job==null){
            return;
        }
        if(job.getJobStatus()==1){
            ScheduleUtils.runJob(scheduler,job);
        }
    }

    /**
     * @author yushuo
     * @description 停止任务
     * @date 14:41 2021/6/23
     * @param [id]
     * @return void
     **/
    public void resumeJob(long id) throws SchedulerException{
        MonitorJob job=getModel(id);
        if(job==null){
            return;
        }
        if(job.getJobStatus()==0){
            ScheduleUtils.resumeJob(scheduler,job);
            LambdaUpdateWrapper<MonitorJob> update= Wrappers.lambdaUpdate();
            update.set(MonitorJob::getJobStatus,1)
                    .eq(MonitorJob::getJobId,id);
            jobMapper.update(null,update);


        }
    }

    /**
     * @author yushuo
     * @description 移除任务
     * @date 14:55 2021/6/23
     * @param [id]
     * @return void
    **/
    public void deletJob(long id){
        MonitorJob job=getModel(id);
        if(job==null){
            return ;
        }
        boolean ret=ScheduleUtils.deleteJob(scheduler,job);
        if(ret){
            jobMapper.deleteById(id);
        }
    }
}




